package MySingList;

import java.util.HashMap;
import java.util.List;

public class MySingList {
    static class ListNode{
        public int val;
        public ListNode next;
        public ListNode(int val){
            this.val = val;
        }
        public ListNode(){

        }
    }
    public ListNode head;
    public void creatListNode(){
        ListNode listNode1 = new ListNode(12);
        ListNode listNode2 = new ListNode(23);
        ListNode listNode3 = new ListNode(34);
        ListNode listNode4 = new ListNode(45);
        ListNode listNode5 = new ListNode(56);
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;
        this.head = listNode1;
    }

    public void display(){
        ListNode cur = head;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    public int size(){
        int count = 0;
        ListNode cur = head;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    public boolean contains(int key){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public void addFirst(int key){
        ListNode cur = new ListNode(key);
        cur.next = head;
        head = cur;
    }

    public void addLast(int key){
        ListNode node = new ListNode(key);
        ListNode cur = head;
        if(cur == null){
            head = cur;
            return;
        }
        while (cur.next!=null){
            cur = cur.next;
        }
        cur.next = node;
    }

    public void addIndex(int index, int data){
        ListNode node = new ListNode(data);
        ListNode cur = head;
        if(index<0 || index>size()){
            System.out.println("位置不合法");
            return;
        }
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == size()){
            addLast(data);
            return;
        }

        int count = index;
        while (count!=0){
            count--;
            cur = cur.next;
        }
        node.next = cur.next.next;
        cur.next = node;

    }

    public void remove(int data){
        ListNode cur = head;
        if(head == null){
            return;
        }
        if(head.val == data){
            head = head.next;
            return;
        }
        while (cur.next.val != data){
            cur = cur.next;
        }
        cur.next = cur.next.next;
    }

    public void removeAllKey(int key) {
        ListNode prev = head;
        ListNode cur = head.next;
        if(head == null){
            return;
        }
        while (cur != null){
            if(cur.val == key){
                prev.next = cur.next;
                cur = cur.next;
            }else{
                prev = cur;
                cur = cur.next;
            }
        }
        if(head.val == key){
            head = head.next;
        }
    }

    public void clear(){
        this.head = null;
    }


    //反转链表
    public ListNode reverseList(){
        if(head == null){
            return null;
        }
        if (head.next == null){
            return head;
        }
        ListNode cur = head.next;
        head.next = null;
        ListNode curNext = null;
        while (cur!=null){
            curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }

    public void display(ListNode newHead){ //方法重载
        ListNode cur = newHead;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    public ListNode middleNode(){
        ListNode fast = head;
        ListNode slow = head;
        if(head == null){
            return null;
        }
        if (head.next == null){
            return head;
        }
        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    public ListNode FindkthToTal(ListNode head, int k){
        if(head == null || k <= 0){
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        for (int i=0; i<k-1; i++){
            fast = fast.next;
            if (fast == null){
                return null;
            }
        }
        while (fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }

    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode headA = list1;
        ListNode headB = list2;
        ListNode newHead = new ListNode();
        ListNode tmpHead = newHead;
        while (headA != null && headB != null){
            if(headA.val >= headB.val){
                tmpHead.next = headA;
                headA = headA.next;
            }else {
                tmpHead.next = headB;
                headB = headB.next;
            }
            tmpHead = tmpHead.next;
        }
        if(headA != null){
            tmpHead.next = headA;
        }
        if(headB != null){
            tmpHead.next = headB;
        }
        return newHead.next;
    }
}
